<template>
  <div class="Ad">
    <!-- 查询列表 -->
    <el-row class="Ad-list">
      <el-col :span="24">
        <el-table :data="indexImg" border style="width: 100%">
          <el-table-column prop="title" label="名称" width="100">
          </el-table-column>
          <el-table-column prop="url" label="图片" width="120">
            <template slot-scope="scope">
              <img :src="scope.row.url" height="50" />
            </template>
          </el-table-column>

          <el-table-column prop="address" label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="default" @click="getInfo(scope.row)"
                >添加/修改</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 添加或编辑关键词 -->
    <el-dialog
      :title="type == 0 ? '添加' + title : '修改' + title"
      :visible.sync="AdShow"
    >
      <el-form :model="AdForm" :rules="AdRules" ref="AdForm">
        <el-form-item label="图片" label-width="120px" prop="url">
          <el-upload
            ref="upload"
            :action="uploadbaseurl + '/upload'"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :limit="1"
            :on-remove="handleRemove"
            :on-success="uploadSuccess"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="AdSubmit('AdForm')" type="primary">{{
          type == 0 ? "添 加" : "修 改"
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      indexImg: [
        {
          i: 0,
          title: "轮播图1",
          url: "",
        },
        {
          i: 1,
          title: "轮播图2",
          url: "",
        },
        {
          i: 2,
          title: "29.9套餐",
          url: "",
        },
        {
          i: 3,
          title: "会员中心",
          url: "",
        },
        {
          i: 4,
          title: "充值",
          url: "",
        },
        {
          i: 5,
          title: "邀请",
          url: "",
        },
      ],
      title: "",
      type: 0,
      uploadbaseurl: "",
      // 上传
      dialogImageUrl: "",
      dialogVisible: false,
      // 弹出层
      AdShow: false,
      AdForm: {
        pic: "",
        type: 2,
      },
      AdRules: {
        pic: [{ required: true, message: "请选择图片", trigger: "change" }],
      },
    };
  },
  created() {
    this.initData();
    this.uploadbaseurl = this.baseurl;
  },
  methods: {
    async initData() {
      this.AdShow = false;
      let res = await this.http.get(
        "https://app.aseh.com.cn/web/IndexGoods/getOtherPic"
      );
      console.log(res);
      if (res.result) {
        JSON.parse(res.result.pic).forEach((e, i) => {
          this.indexImg[i].url = e;
        });
        this.type = 2;
        this.AdForm.id = res.result.id;
      }
    },
    getInfo(obj) {
      console.log(obj);
      this.AdShow = true;
      this.AdForm.i = obj.i;
      this.title = obj.title;
    },
    async AdSubmit(formName) {
      let param = { ...this.AdForm };
      if (this.AdForm.pic) {
        let del = await this.http.get("/pic/delete?id=" + this.AdForm.id);
        if (del.code != 200) {
          this.eui.Message({ message: del.msg, type: "error" });
        }
        let res = await this.http.post("/pic/addAndUpdatePic", param);
        if (!res.error) {
          this.eui.Message({
            message: this.type == 0 ? "添加成功" : "修改成功",
            type: "success",
          });

          this.AdShow = false;
          this.$refs.upload.clearFiles();

          this.initData();
        } else {
          this.eui.Message({ message: res.msg, type: "error" });
        }
      } else {
        console.log("error submit!!");
        return false;
      }
    },
    async deleteCourse() {},
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    uploadSuccess(response, file, fileList) {
      let imgs = [];
      this.indexImg[this.AdForm.i].url = response.result[0];
      this.dialogImageUrl = "";
      this.indexImg.forEach((e) => {
        imgs.push(e.url);
      });
      this.AdForm.pic = JSON.stringify(imgs);
    },
  },
};
</script>

<style scoped>
.Ad {
  width: 100%;
  min-height: 100%;
  padding: 15px;
  box-sizing: border-box;
}
.Ad-Info {
  width: 100%;
  margin-bottom: 15px;
}
.Ad-Info-li {
  width: 100%;
}
.Ad-list {
  width: 100%;
  margin-bottom: 20px;
}
.Ad-page-box {
  display: flex;
  justify-content: flex-end;
}
</style>